package com.android.ailaoshan.index.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.ailaoshan.R;
import com.android.ailaoshan.common.BaseApplication;
import com.android.ailaoshan.index.map.MapConfig;
import com.android.ailaoshan.index.map.MapPath;
import com.android.ailaoshan.picture.DealPicture;
import com.esri.arcgisruntime.mapping.view.MapView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class MapFragment extends Fragment {

    private Context mContext;

    private View mapFragment;
    private MapView mMapView;

    private MapPath mapPath;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        mapFragment = inflater.inflate(R.layout.map_fragment,container,false);

        return mapFragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mMapView = mapFragment.findViewById(R.id.mapView_content);
    }


    @Override
    public void onStart(){
        super.onStart();

        MapConfig mapConfig = new MapConfig(mapFragment, mMapView);
        mapConfig.initMap();
        mapConfig.mapListener(mContext,MapFragment.this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        BaseApplication application = BaseApplication.getInstance();
        switch (requestCode){
            case 201:{
                DealPicture dealPicture = new DealPicture();
                dealPicture.donePicture(resultCode,application.getImageFile(),mContext);
                break;
            }
        }
    }

    @Override
    public void onResume(){
        super.onResume();
        mMapView.resume();

        mapPath = new MapPath(mapFragment,mMapView);
        mapPath.init();
    }

    @Override
    public void onPause(){
        mMapView.pause();
        super.onPause();

        if(mapPath != null){
            mapPath.unRegisterLocationChangeReceiver();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.dispose();
    }
}
